package com.doform.android.xianggucheng.activity;

import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.doform.android.xianggucheng.R;
import com.doform.android.xianggucheng.bean.CategoryBean;
import com.doform.android.xianggucheng.popwindow.CommonPopWindow;
import com.doform.android.xianggucheng.retrofit.subscriber.ErrorHandleSubscriber;
import com.doform.android.xianggucheng.retrofit.HttpManager;
import com.doform.android.xianggucheng.utils.TsUtils;
import com.doform.android.xianggucheng.widget.DividerItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.annotations.NonNull;

public class UpdateArticleCategoryActivity extends BaseTitleActivity {

    @BindView(R.id.tv_category)
    TextView mCategory;

    @BindView(R.id.ll)
    LinearLayout mLl;

    @BindView(R.id.et_name)
    EditText etName;

    @BindView(R.id.et_sort)
    EditText etSort;

    private String oneName;
    private String id;
    private String fid;

    private CommonPopWindow mPopupWindow2;
    private RecyclerView mCategoryRecyclerView;
    private List<CategoryBean.ListBean> categoryDatas = new ArrayList<>();
    private CommonAdapter<CategoryBean.ListBean> mCommonAdapter;

    @Override
    protected int layoutResId() {
        return R.layout.activity_new_and_update_category;
    }

    @Override
    protected void init() {

        if (mPopupWindow2 == null) {
            mPopupWindow2 = new CommonPopWindow(this, R.layout.pop_goods_category);
        }

        oneName = getIntent().getStringExtra("shangji_name");

        id = getIntent().getStringExtra("id");
        fid = getIntent().getStringExtra("fid");
    }

    @Override
    protected void showView() {
        titleBar.setTextTitle("修改分类");

        if (oneName == null) {
            mCategory.setText("-请选择-");
        } else {
            mCategory.setText(oneName);
        }
        etName.setText(getIntent().getStringExtra("name"));
        etSort.setText(getIntent().getStringExtra("corder"));

        etSort.requestFocus();
        etName.requestFocus();
    }

    @Override
    protected void initView() {
        mCategoryRecyclerView = mPopupWindow2.getView(R.id.recyclerView);
        mCategoryRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        mCategoryRecyclerView.setItemAnimator(new DefaultItemAnimator());
        mCategoryRecyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL_LIST));
    }

    @Override
    protected void initData() {
        initCategoryData();
    }

    private void initCategoryData() {
        HttpManager.instance()
                .getCategory2("1")
                .subscribe(new ErrorHandleSubscriber<CategoryBean>() {
                    @Override
                    public void onNext(@NonNull CategoryBean categoryBean) {
                        categoryDatas.addAll(categoryBean.getList());
                    }
                });
    }

    @Override
    protected void initListener() {
        initCateListener();

    }

    private void initCateListener() {

        mCommonAdapter = new CommonAdapter<CategoryBean.ListBean>(this, R.layout.pop_category_item2, categoryDatas) {
            @Override
            protected void convert(ViewHolder holder, CategoryBean.ListBean dataBean, int position) {
                holder.getView(R.id.rl_select).setBackgroundColor(getResources().getColor(R.color.background));
                TextView view = holder.getView(R.id.tv);
                view.setText(dataBean.getClsname());
            }
        };

        mCommonAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                CategoryBean.ListBean dataBean = categoryDatas.get(position);
                fid = String.valueOf(dataBean.getId());

                mCategory.setText(dataBean.getClsname());
                mPopupWindow2.dismiss();

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
        mCategoryRecyclerView.setAdapter(mCommonAdapter);
    }


    private void submit() {

        String mName = etName.getText().toString().trim();
        String mSort = etSort.getText().toString().trim();

        if (TextUtils.isEmpty(mName)) {
            TsUtils.showToast("请输入分类名称");
            return;
        } else if (TextUtils.isEmpty(mSort)) {
            mSort = "255";
        } else if (TextUtils.isEmpty(fid)) {
            fid = "0";
        }
        postData(mName, mSort, id, fid);
    }


    private void postData(String mName, String mSort, String id, String fid) {
        HttpManager.instance()
                .updateCategory2(mName, mSort, id, fid)
                .subscribe(new ErrorHandleSubscriber<String>() {
                    @Override
                    public void onNext(@NonNull String str) {
                        finish();
                    }
                });
    }

    @OnClick({R.id.btn_submit, R.id.tv_category})
    public void click(View v) {
        switch (v.getId()) {
            case R.id.btn_submit:
                submit();
                break;
            case R.id.tv_category:
                if (!TextUtils.isEmpty(fid)) {
                    mPopupWindow2.showAtLocation(mLl, Gravity.BOTTOM, 0, 0);
                }
                break;
            default:
                break;
        }
    }
}


